(* COULEUR *)

Graphics.clear_graph () ;;

# load "graphics.cma" ;;

type couleur = Noir| Blanc| Rouge| Vert| Bleu| Random ;;

let my_set_color a b c = Graphics.set_color (Graphics.rgb a b c) ;;

Graphics.open_graph "784x806" ;;

Random.self_init() ;;

let color = function
  | Noir -> my_set_color  0 0 0
  | Blanc -> my_set_color 255 255 255
  | Rouge -> my_set_color 255 0 0
  | Vert -> my_set_color 0 255 0
  | Bleu -> my_set_color 0 0 255
  | Random -> my_set_color (Random.int(255)) (Random.int(255)) (Random.int(255)) ;;

let draw_rect c x y w h =
  let _ = color c in
    Graphics.fill_rect x y w h ;;

type image =
  | Uni of couleur
  | Comp of image*image*image*image*image*image*image*image ;;

let rec display img x y w h = match img with
  | Uni (c) -> draw_rect c x y w h
  | Comp (i1, i2, i3, i4, i5, i6, i7, i8) -> display i1 x y (w/3) (h/3); (*bas gauche*)
                                                 display i2 (x+w/3) y (w/3) (h/3); (*bas milieu*)
                                                 display i3 (x+2*w/3) y (w/3) (h/3); (*bas droite*)
                                                 display i4 x (y+h/3) (w/3) (h/3); (*milieu gauche*)
                                                 display i5 (x+2*w/3) (y+h/3) (w/3) (h/3); (*milieu droit*)
                                                 display i6 x (y+2*h/3) (w/3) (h/3); (*haut gauche*)
                                                 display i7(x+w/3) (y+2*h/3) (w/3) (h/3); (*haut milieu*)
                                                 display i8 (x+2*w/3) (y+2*h/3) (w/3) (h/3);; (*haut droite*) 

let magic img = display img 0 0 (Graphics.size_x()) (Graphics.size_y()) ;;

let image_simple = Comp (
  Uni (Random),
  Uni (Random),
  Uni (Random),
  Uni (Random),
  Uni (Random),
  Uni (Random),
  Uni (Random),
  Uni (Random)
) ;;

let rec frac8 img = Comp (img, img, img, img,img, img, img, img) ;;

let rec apply f img n = match n with
  | 1 -> f img
  | n -> 
    if n>0 then
     apply f (f img) (n-1)
    else
     invalid_arg "n droit etre strictement positif" ;;

magic (apply (frac8) image_simple 4) ;;



